package com.zzvcom.webapp.action.admin;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.zzvcom.persist.model.admin.Gallery;
import com.zzvcom.service.admin.GalleryManager;
import com.zzvcom.template.ResultPage;
import com.zzvcom.utils.ExtMessage;

@Controller
@Scope("prototype")
public class GalleryAction
{

	private Gallery gallery;

	private GalleryManager manager;

	private ExtMessage msg = new ExtMessage();// 返回信息

	private Map rs = new HashMap();// 结果集合

	private ResultPage resultPage;

	private Integer start;

	private Integer limit;

	public String execute()
	{
		if (resultPage == null)
		{
			resultPage = new ResultPage();
		}
		resultPage.startLimitSetting(start, limit);
		DetachedCriteria dc = DetachedCriteria.forClass(Gallery.class);
		dc.addOrder(Order.desc("seq"));
		resultPage.setDetachedCriteria(dc);
		manager.getResultPageByDetachedCriteria(resultPage);
		rs.put("root", resultPage.getResult());
		rs.put("records", resultPage.getTotalRecord());
		return "rs";
	}

	public String save()
	{
		if (StringUtils.isBlank(gallery.getId()))
		{
			gallery.setId(null);
		}
		manager.save(gallery);
		msg.setInfo("保存成功");
		return "msg";
	}

	public String delete()
	{
		gallery = manager.get(gallery.getId());
		manager.delete(gallery);
		msg.setInfo("删除成功");
		return "msg";
	}

	public Gallery getGallery()
	{
		return gallery;
	}

	public void setGallery(Gallery gallery)
	{
		this.gallery = gallery;
	}

	public ExtMessage getMsg()
	{
		return msg;
	}

	public void setMsg(ExtMessage msg)
	{
		this.msg = msg;
	}

	public Map getRs()
	{
		return rs;
	}

	public void setRs(Map rs)
	{
		this.rs = rs;
	}

	public ResultPage getResultPage()
	{
		return resultPage;
	}

	public void setResultPage(ResultPage resultPage)
	{
		this.resultPage = resultPage;
	}

	public Integer getStart()
	{
		return start;
	}

	public void setStart(Integer start)
	{
		this.start = start;
	}

	public Integer getLimit()
	{
		return limit;
	}

	public void setLimit(Integer limit)
	{
		this.limit = limit;
	}

	public GalleryManager getManager()
	{
		return manager;
	}

	@Autowired
	public void setManager(GalleryManager manager)
	{
		this.manager = manager;
	}

}
